package cliente;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import cliente.Principal;
import elementos.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

/**
 *
 * @author julio
 */
public class AnimationController extends Thread implements MouseListener,GoalListener,KillBrickListener{
    private Composite scene;
    private ArrayList<Movible> objects;
    private boolean stopping = false;
    private Principal applet;
    
    private ArrayList<BarController> bars;
    
    public void exit(){
        stopping=true;
    }
    
    public AnimationController(Composite s,Principal a){
        scene = s;
        applet=a;
        boolean primera=true;
        //Create List of Balls.
        objects = new ArrayList<Movible>();
        bars= new ArrayList<BarController>();
        for (int i = 0; i < scene.size(); i++) {
            if (scene.getChild(i) instanceof Movible) {
                objects.add((Movible) scene.getChild(i));
            }else if (scene.getChild(i) instanceof Bar) {
                BarController b;
                if (primera){
                    b = new BarController((Bar) scene.getChild(i), scene, 0, 0);
                    primera=false;
                }else{
                    b = new BarController((Bar) scene.getChild(i), scene, KeyEvent.VK_UP, KeyEvent.VK_DOWN);
                }
                bars.add(b);
                ((Bar) scene.getChild(i)).addKillBricklListener(this);
                applet.getVjuego().addKeyListener(b);
                b.start();
            }
        }
        
    }
    
    public void run(){
        while (!stopping) {
            try {
                //move all balls
                for (int i = 0; i < objects.size(); i++) {
                    //move each ball.
                    Movible b = (Movible) objects.get(i);
                    b.moveObject();    
                     if(scene.collision(b)!=null) {
                        sleep(10);
                    }  
                }
                sleep(10);
            } catch (InterruptedException ex) {
                
            }
        }
        for(int i=0;i<bars.size();i++){
            bars.get(i).exit();
            break;
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        /*float x = e.getX(), y = e.getY();
        if (e.getButton()==1){
            //on click add a ball
            Ball b2 =null;
            for(int i=0;i<objects.size();i++){
                if(objects.get(i)instanceof Ball){
                    b2=(Ball)objects.get(i);
                    break;
                }
            }
            float dx=b2.getDX(),dy=b2.getDY(),radio=b2.getRadio();
            Ball b = new Ball(x, y, dx, dy, radio);
            if (scene.collision(b)==null){
                objects.add(b);
                scene.add(b);
            }
        }else if (e.getButton()==3){
            //add a brick
            Brick b1 = new Brick(x, y, 50, 50);
            objects.add(b1);
            scene.add(b1);
        }        */
    }

    @Override
    public void mousePressed(MouseEvent e) {
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        
    }

                   
       /*for (int i = 0; i < scene.size(); i++) {
            if (scene.getChild(i) instanceof Bar) {
               ((Bar)scene.getChild(i)).moveDown();
            }
            * 
            * 
            * Bar player1=null,player2=null;
        for (int i = 0; i < scene.size(); i++) {
            if (scene.getChild(i) instanceof Bar) {
                
               if(((Bar)scene.getChild(i)).getNumber()==0){
                   player1=(Bar)scene.getChild(i);
               }else{
                   player2=(Bar)scene.getChild(i);
               }
            }
        }
        switch(ke.getKeyCode()){
            case KeyEvent.VK_DOWN:
                if(player1!=null)player1.moveUp();
                break;

            case KeyEvent.VK_UP:
                player1.moveDown();
                break;
                
            case KeyEvent.VK_S:
                player2.moveUp();
                break;
                
            case KeyEvent.VK_W:
                player2.moveDown();
                break;
                
            default:
                break;
        }*/

    @Override
    public void onGoal(Ball b) {
        int count=0;
        //count the number of balls
        for(int i=0;i<objects.size();i++){
            if(objects.get(i)instanceof Ball){
                count++;
            }
        }
        //remove the ball or place it at center
        if(count>1){
            objects.remove(b);
            scene.remove(b);
        }else{
            center(b);
        }
    }
    
    private void center(Ball b){
        float x=applet.getWidth()/2;
        float y=applet.getHeight()/2;
        b.moveTo(x, y);
    }

    @Override
    public void onKill(Brick b) {
        Bar b2 = (Bar) bars.get(1).getPlayer();
        if (b2.BrickCollision(b)){
            b2.perderVida();
            applet.vidaPerdida();
        }
        this.objects.remove(b);
        this.scene.remove(b);
    }
    
   
}
